﻿@model EntityViewModel
@{
    ViewBag.Title = Model.Metadata.Name + " List";
    object area = ViewContext.RouteData.DataTokens["Area"];
    if (area == null)
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    else
    {
        Layout = "~/Areas/" + area.ToString() + "/Views/Shared/_Layout.cshtml";
    }
}
<ol class="breadcrumb">
    <li><a href="@Url.Action("Index","Home")">Home</a></li>
    <li class="active">@Model.Metadata.Name</li>
</ol>
@if (Model.SearchItem.Length > 0)
{
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <p><h4>Search content</h4></p>
        @for (int i = 0; i < Model.SearchItem.Length; i++)
        {
            Html.RenderPartial("SearchItem", Model.SearchItem[i]);
        }
    </div>
}
<div class="row">
    @if (Model.Parent != null)
    {
        <link href="~/Content/jstree.css" rel="stylesheet" />
        <script src="~/Scripts/jstree.js"></script>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">@(Model.Metadata.Name)Navigation</div>
                <div class="panel-body" id="parentview">
                    <ul>
                        <li class="jstree-open">
                            <a data-nav="true" href="@Html.Raw(Url.Action("Index"))">All</a>
                            <ul>
                                @{Html.RenderPartial("ParentView", Model.Parent);}
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            $('#parentview').bind('select_node.jstree', function (node, selected) {
                window.location.href = selected.node.a_attr.href;
            });
            $('#parentview').jstree();
        </script>
    }
    <div class="@(Model.Parent == null ? "col-md-12":"col-md-9")">
        <div class="panel panel-default">

            <!-- Default panel contents -->
            <div class="panel-heading">
                @(Model.Metadata.Name) List
            </div>
            <!-- Table -->
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            @for (int i = 0; i < Model.Headers.Length; i++)
                            {
                                <td title="@Model.Headers[i].Description">@(Model.Headers[i].ShortName ?? Model.Headers[i].Name)</td>
                            }
                            <td>Operation</td>
                        </tr>
                    </thead>
                    <tbody>
                        @for (int i = 0; i < Model.Items.Length; i++)
                        {
                            <tr>
                                @for (int b = 0; b < Model.Headers.Length; b++)
                                {
                                    <td>@EntityValueConverter.GetConverter(Model.Headers[b]).ConvertToString(Model.Headers[b].Property.GetValue(Model.Items[i]))</td>
                                }
                                <td class="text-center" style="width: 64px;">
                                    <div class="btn-group btn-group-xs text-left">
                                        <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
                                            <span class="caret"></span>
                                        </a>
                                        <ul class="dropdown-menu">
                                            <li><a class="btn" href="@Html.Raw(Url.Action("Detail", new { id = Model.Items[i].Index }))"><i class="glyphicon glyphicon-list-alt"></i>Detail</a></li>
                                            @if (Model.Metadata.EditRoles.All(t => User.IsInRole(t)))
                                            {
                                                <li><a class="btn" href="@Html.Raw(Url.Action("Edit", new { id = Model.Items[i].Index }))"><i class="glyphicon glyphicon-edit"></i>Edit</a></li>
                                            }
                                            @if (Model.Metadata.RemoveRoles.All(t => User.IsInRole(t)))
                                            {
                                                <li><a class="btn" href="javascript:remove('@Model.Items[i].Index')"><i class="glyphicon glyphicon-remove"></i>Delete</a></li>
                                            }
                                            @for (int c = 0; c < Model.Buttons.Length; c++)
                                            {
                                                MvcViewButton button = Model.Buttons[c] as MvcViewButton;
                                                if (button != null && button.GetLink != null)
                                                {
                                                    <li><a href="@Html.Raw(button.GetLink(Url, Model.Items[i]))">@Html.Raw(button.Icon)@Html.Raw(button.Name)</a></li>
                                                }
                                            }
                                        </ul>
                                    </div>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                <div class="row">
                    <div class="col-md-4">
                        @{Html.PaginationButton(Model);}
                    </div>
                    <div class="col-md-4 text-center">
                        <div class="btn-group">
                            @if (Model.Metadata.AddRoles.All(t => User.IsInRole(t)))
                            {
                                <a href="@Html.Raw(Url.Action("Create"))" class="btn btn-default">Create</a>
                            }
                            @if (Model.Metadata.SearchProperties.Length > 0)
                            {
                                <a href="@Html.Raw(Url.Action("Search"))" class="btn btn-default">Search</a>
                            }
                            @for (int i = 0; i < Model.Buttons.Length; i++)
                            {
                                MvcViewButton button = Model.Buttons[i] as MvcViewButton;
                                if (button != null && button.StaticLink != null)
                                {
                                    <a href="@Html.Raw(button.StaticLink)" class="btn btn-default">@Html.Raw(button.Icon)@Html.Raw(button.Name)</a>
                                }
                            }
                        </div>
                    </div>
                    <div class="col-md-4 text-right">
                        @{Html.Pagination(Model);}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Result</h4>
            </div>
            <div class="modal-body">
                <p>This item is reference by other item, can not delete.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function remove(id) {
        $.ajax({
            url: '@Html.Raw(Url.Action("Remove"))/' + id,
            cache: false,
            type: 'POST',
            success: function () {
                location.reload();
            },
            error: function () {
                $('#myModal').modal('show');
            }
        })
    }
</script>